package com.cskaoyan.controller.front.goods;

import com.cskaoyan.bean.po.BaseParam;

import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.front.goodsvo.*;
import com.cskaoyan.service.front.goods.WxGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;


/**
 * @program: SSMItem
 * @description 该controller实现商品相关功能
 * @author: YY
 * @create: 2022-10-14 14:30
 **/
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Autowired
    WxGoodsService wxGoodsService;

    /**
     * 商品总数
     *
     * @return
     */
    @GetMapping("count")
    public BaseRespVo goodsCount() {

        Integer count = wxGoodsService.selectGoodsCount();
        return BaseRespVo.ok(count);
    }

    /**
     * 进入商品类目
     *
     * @param id
     * @return
     */
    @GetMapping("category")
    public BaseRespVo goodsCategory(Integer id) {


        CategoryVo categoryVo = wxGoodsService.selectGoodsByCategory(id);
        return BaseRespVo.ok(categoryVo);

    }

    /**
     * http://182.92.235.201:8083/wx/goods/list?categoryId=1008008&page=1&limit=10
     * http://182.92.235.201:8083/wx/goods/list?brandId=1001000   &page=1&limit=10
     * http://182.92.235.201:8083/wx/goods/list?keyword=1&page=1&limit=20&sort=name&order=desc&categoryId=0
     * ttp://182.92.235.201:8083/wx/goods/list?keyword=1&page=1&limit=20&sort=retail_price&order=desc&categoryId=0
     * http://localhost:8083/wx/goods/list?     keyword=1&page=1&limit=20&sort=add_time&order=desc&categoryId=1010000
     * <p>
     * categoryId:类别id
     * brandId：品牌标识id
     */
    @GetMapping("list")
    public BaseRespVo GoodsList(Integer categoryId, Integer brandId, Integer page, Integer limit, String keyword, BaseParam baseParam, HttpServletRequest request) {

        //进入品牌商详情
        if (brandId != null) {
            GoodsListBrandVo goods = wxGoodsService.selectGoodsListVoBrand(brandId, page, limit);
            goods.setPage(page);
            goods.setLimit(limit);
            return BaseRespVo.ok(goods);
        }


        //判断是否是排序
        if (Objects.equals(baseParam.getSort(), "retail_price") || Objects.equals(baseParam.getSort(), "add_time")) {
            GoodsListBrandVo1 goodsListBrandVo = wxGoodsService.selectGoodsOrderByRetailPrice(baseParam, keyword, categoryId);
            return BaseRespVo.ok(goodsListBrandVo);
        }

        //搜索
        if (keyword != null) {
            GoodsListBrandVo goods = wxGoodsService.selectGoodsByKeywords(keyword, baseParam, request);
            return BaseRespVo.ok(goods);
        }
//        if (categoryId != null && Objects.equals(baseParam.getSort(), "retail_price") && keyword == null) {
//            GoodsListCategoryVo goods = wxGoodsService.selectGoodsListVoCategory(categoryId, limit, page);
//            goods.setPage(page);
//            goods.setLimit(limit);
//            return BaseRespVo.ok(goods);
//        }

        //综合
//          if (Objects.equals(baseParam.getSort(),"name")){
//            GoodsListCategoryVo goods = wxGoodsService.selectGoodsListVoCategory(categoryId, limit, page);
//            goods.setPage(page);
//            goods.setLimit(limit);
//            return BaseRespVo.ok(goods);
//        }
        else {
            GoodsListCategoryVo goods = wxGoodsService.selectGoodsListVoCategory(categoryId, limit, page);
            goods.setPage(page);
            goods.setLimit(limit);
            return BaseRespVo.ok(goods);
        }

    }


    /**
     * 显示商品详情
     *
     * @param id：商品id
     * @return：前端所需数据
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        // 获取当前用户id
        Integer userId = 0;
        try {
            userId = Integer.parseInt((String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 根据id从service层查询数据并返回
        GoodsDetailVo goodsDetailVo = wxGoodsService.detail(id, userId);
        // 封装后返回数据给前端
        return BaseRespVo.ok(goodsDetailVo);
    }


    /**
     * http://localhost：8080/wx/goods/related?id=1181085
     *
     * @param id
     * @return
     */
    @GetMapping("related")
    public BaseRespVo goodsRelated(Integer id) {

        GoodsRelatedVo goodsList = wxGoodsService.selectRelatedGoods(id);


        return BaseRespVo.ok(goodsList);


    }


}
